package com.chinacoal.microservice.impl.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.TaskExtAttrMapRegisterClient;
import com.chinacoal.microservice.api.model.QueryUtaskCatAttrMapParam;
import com.chinacoal.microservice.api.model.entity.UtaskCatAttrMapEntity;
import com.chinacoal.microservice.impl.service.UtaskCatAttrMapService;
import com.chinacoal.microservice.login.LoginUser;
import com.chinacoal.microservice.model.IamUser;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;

@RestController
public class UtaskCatMapController implements TaskExtAttrMapRegisterClient {

    @Resource
    private UtaskCatAttrMapService utaskCatAttrMapService;
    @Resource
    private LoginUser loginUser;
    
	@Override
	public Result<Object> insert(UtaskCatAttrMapEntity utaskCatAttrMapEntity) {
    	try {
    		IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new Exception("用户未登录");
    		
			// 数据补全
			utaskCatAttrMapEntity.setCreationDate(new Date());
			utaskCatAttrMapEntity.setCreatedBy(iamUser.getUserId());
			utaskCatAttrMapEntity.setCreatedByName(iamUser.getName());
    		
			// 执行新增
			utaskCatAttrMapService.insertUtaskCatAttrMap(utaskCatAttrMapEntity);
			
			return Result.success("新增待办类型映射成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Object> update(UtaskCatAttrMapEntity utaskCatAttrMapEntity) {
		try {
    		IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new Exception("用户未登录");
    		
			// 数据补全
			utaskCatAttrMapEntity.setLastUpdateDate(new Date());
			utaskCatAttrMapEntity.setLastUpdatedBy(iamUser.getUserId());
			utaskCatAttrMapEntity.setLastUpdatedByName(iamUser.getName());
    		
			// 执行更新
			utaskCatAttrMapService.updateUtaskCatAttrMap(utaskCatAttrMapEntity);
			
			return Result.success("更新待办类型映射成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Object> delete(String mapId) {
		try {
			// 执行删除
			utaskCatAttrMapService.deleteUtaskCatAttrMap(mapId);
			return Result.success("删除待办类型映射成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Page<UtaskCatAttrMapEntity>> findList(Integer page, Integer pageSize, QueryUtaskCatAttrMapParam param) {
		try {
    		Page<UtaskCatAttrMapEntity> utaskCatAttrMapPage = utaskCatAttrMapService.findList(page, pageSize, param);
			return Result.success(utaskCatAttrMapPage);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<UtaskCatAttrMapEntity> findOne(String mapId) {
		try {
			UtaskCatAttrMapEntity utaskCatAttrMap = utaskCatAttrMapService.selectById(mapId);
			return Result.success(utaskCatAttrMap);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<List<UtaskCatAttrMapEntity>> getCatAttrMap(String sysId, String catId, String attrId) {
		try {
			List<UtaskCatAttrMapEntity> utaskCatAttrMapList = utaskCatAttrMapService.getCatAttrMap(sysId,catId,attrId);
			return Result.success(utaskCatAttrMapList);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
    
}

